package com.example.sharesinfo.ui;


import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.ImageButton;

import com.androidkun.xtablayout.XTabLayout;
import com.example.sharesinfo.R;
import com.example.sharesinfo.adapter.MyPagerAdapter;
import com.example.sharesinfo.base.BaseUIActivity;
import com.example.sharesinfo.bean.TingFuPaiBean;
import com.example.sharesinfo.fragment.TingFuPaiFragment;
import com.example.sharesinfo.utils.LogUtils;
import com.example.sharesinfo.utils.OkHttpUtils;
import com.google.android.material.tabs.TabLayout;
import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Arrays;

import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

public class TingFuPaiActivity extends BaseUIActivity implements View.OnClickListener {
    private static final String LOGTAG = "TingFuPaiActivity:";
    private static final int GetSuccess = 1001;
    private static final int GetFailed = 1002;
    private ImageButton ib_back;
    private XTabLayout tabLayout;
    private ViewPager viewpager;
    private MyPagerAdapter pagerAdapter;
    private ArrayList<Fragment> fragments = new ArrayList<>();
    private String[] titles = new String[]{"停牌", "复牌", "首发上市", "网上发行", "资格登记", "增发新股", "分红转增", "除权除息", "退市风险", "配股股权", "配股上市"};
    private TingFuPaiBean bean;

    private Handler mHandler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            switch (msg.what) {
                case GetSuccess:
                    bean = (TingFuPaiBean) msg.obj;
                    for (int i = 0; i < titles.length; i++) {
                        fragments.add(new TingFuPaiFragment(i, bean));
                        tabLayout.addTab(tabLayout.newTab());
                    }
                    pagerAdapter = new MyPagerAdapter(getSupportFragmentManager(), TingFuPaiActivity.this, fragments, Arrays.asList(titles));
                    viewpager.setAdapter(pagerAdapter);
                    tabLayout.setupWithViewPager(viewpager);
                    break;
                case GetFailed:
                    break;
            }
            return false;
        }
    });

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ting_fu_pai);
        initView();
        GetStopRecoverStockList();
    }

    private void initView() {
        ib_back = findViewById(R.id.ib_back);
        ib_back.setOnClickListener(this);
        tabLayout = findViewById(R.id.tabLayout);
        viewpager = findViewById(R.id.viewpager);

    }

    /**
     * 获取停复牌
     */
    public void GetStopRecoverStockList() {
        String url = "http://119.23.221.142/api/stock/GetStopRecoverStockList";
        OkHttpUtils.getInstance().Get(url, new OkHttpUtils.okHttpListener() {
            Message message = mHandler.obtainMessage();

            @Override
            public void OnSuccess(String s, int code) throws JSONException {
                JSONObject object = new JSONObject(s);
                if (object.getString("code").equals("200")) {
                    TingFuPaiBean bean = new Gson().fromJson(s, TingFuPaiBean.class);
                    message.what = GetSuccess;
                    message.obj = bean;
                    mHandler.sendMessage(message);
                } else {
                    message.what = GetFailed;
                    mHandler.sendMessage(message);
                }
            }

            @Override
            public void OnFailed(String s) {
                message.what = 404;
                message.obj = s;
                mHandler.sendMessage(message);
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            default:
                break;
            case R.id.ib_back:
                finish();
                break;
        }
    }
}
